<?php
namespace Admin\Controller;
use Think\Controller;
class FirstController extends CommonController {
	public function index(){
		$this -> assign('title','广告管理');
		$mod = M('com_advist');
		$data = $mod -> where('1=1') -> select();
		//分配
		$this -> assign('data',$data);
		$this -> display('');
	}

	public function add(){
		$this -> assign('title','新闻添加');
		$mod = M('com_advist');
		$com_advist = $mod -> where('adv_id = "%d" '.I('get.'));
		$this -> display('');
	}
	public function edit(){
		$this -> assign('title','广告修改');
		$mod = M('com_advist');	
		$advist = $mod -> where('adv_id = '.I('get.advist_id')) -> find();
		$this -> assign('ad',$advist);
		$this -> display();
	}
	public function guanggaoxiugai(){
		$mod = M('com_advist');
		$mod -> startTrans();
		//获取上传图片的信息
			$config = array(
					'maxSize'=>3145728,
					'rootPath' =>'./Public/imgzip/',
					'saveName' => array('uniqid',''),
					'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
					'autoSub'  => true,
					'subName'  => array('date','Ymd'),
			
			);
			$upload = new \Think\Upload($config);// 实例化上传类
			
			$upload->rootPath  = './Public/imgzip/'; // 设置附件上传目录// 上传文件
			$info   =   $upload->upload();
			if(!$info) {
				// 上传错误提示错误信息
				$this->error($upload->getError());}else{
					// 上传成功 获取上传文件信息
					foreach($info as $file){
						$pic = $file['savepath'].$file['savename'];
							
					}
				}			
			$_POST['img'] = '/imgzip/'.$pic;
			//封装日志
			$admin_log = M('admin_log');
			$ad_log['user_name'] = $_SESSION['admin']['user_name'];
			$ad_log['log_time'] = time();
			$ad_log['user_id'] = $_SESSION['admin']['user_id'];
			$ad_log['log_info'] = '公告删除操作';
			$ad_log['ip_address']= get_client_ip();
			$res2 = $admin_log -> add($ad_log);
			$res = $mod -> where('adv_id='.I('get.id'))-> data($_POST)->save();
			
			if($res>0 && $res2){
					$mod -> commit();
					$this -> success('修改成功',U('admin/first/index'));
			}else{
					$mod -> rollback();
					$this -> error('修改失败',U('admin/first/index'));
			}
	}



	public function insert(){
		$mod = M('com_advist');
		$mod -> startTrans();
		//获取上传图片的信息
			$config = array(
					'maxSize'=>3145728,
					'rootPath' =>'./Public/imgzip/',
					'saveName' => array('uniqid',''),
					'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
					'autoSub'  => true,
					'subName'  => array('date','Ymd'),
			
			);
			$upload = new \Think\Upload($config);// 实例化上传类
			
			$upload->rootPath  = './Public/imgzip/'; // 设置附件上传目录// 上传文件
			$info   =   $upload->upload();
			if(!$info) {
				// 上传错误提示错误信息
				$this->error($upload->getError());}else{
					// 上传成功 获取上传文件信息
					foreach($info as $file){
						$pic = $file['savepath'].$file['savename'];
							
					}
				}			
			$_POST['img'] = '/imgzip/'.$pic;
			//封装日志
			$admin_log = M('admin_log');
			$ad_log['user_name'] = $_SESSION['admin']['user_name'];
			$ad_log['log_time'] = time();
			$ad_log['user_id'] = $_SESSION['admin']['user_id'];
			$ad_log['log_info'] = '广告插入操作';
			$ad_log['ip_address']= get_client_ip();
			$res2 = $admin_log -> add($ad_log);
			$res = $mod -> add($_POST);
			if($res>0 && $res2>0){
				$mod -> commit();
				$this -> success('添加成功',U('Admin/first/index'));
			}else{
				$mod -> rollback();
				$this -> error('添加失败',U('Admin/first/index'));
			}	
	}
	public function delete(){
		$mod = M('com_advist');
		$mod -> startTrans();
		$res = $mod -> where('where com_adv_id = "%d" ',I('get.advist_id'))->delete();
		//封装日志
		$admin_log = M('admin_log');
			$ad_log['user_name'] = $_SESSION['admin']['user_name'];
			$ad_log['log_time'] = time();
			$ad_log['user_id'] = $_SESSION['admin']['user_id'];
			$ad_log['log_info'] = '公告删除操作';
			$ad_log['ip_address']= get_client_ip();
			$res2 = $admin_log -> add($ad_log);
		if($res>0 && $res2>0){
				$mod -> commit();
				$this -> success('删除成功',U('Admin/first/index'));
			}else{
				$mod -> rollback();
				$this -> error('删除失败',U('Admin/first/index'));
			}	
	}
	//公司管理
	public function company(){
		$this -> assign('title','公司管理');
		$mod = M('company');
		$data = $mod -> where('1=1') ->select();
		$this -> assign('data',$data);
		//获取到出售现金积分的人
		$money = M('market') -> field('mk_id,sell_user_id,sell_user_name,content,sell_num,sell_money,status,sell_time') -> where('status=%d and state_check = 0 ',[0]) -> select();
		$this -> assign('note',$_SESSION['admin']['note']);
		
		$this -> assign('money',$money);
		$this -> display('');
	}
	public function update(){
		$mod = M('company');
		$mod -> startTrans();
		//获取上传图片的信息
			$config = array(
					'maxSize'=>3145728,
					'rootPath' =>'./Public/imgzip/',
					'saveName' => array('uniqid',''),
					'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
					'autoSub'  => true,
					'subName'  => array('date','Ymd'),
			
			);
			$upload = new \Think\Upload($config);// 实例化上传类
			
			$upload->rootPath  = './Public/imgzip/'; // 设置附件上传目录// 上传文件
			$info   =   $upload->upload();
			if(!$info) {
				// 上传错误提示错误信息
				$this->error($upload->getError());}else{
					// 上传成功 获取上传文件信息
					foreach($info as $file){
						$pic = $file['savepath'].$file['savename'];
							
					}
				}			
			$_POST['logo'] = '/imgzip/'.$pic;
			//封装日志
			$admin_log = M('admin_log');
			$ad_log['user_name'] = $_SESSION['admin']['user_name'];
			$ad_log['log_time'] = time();
			$ad_log['user_id'] = $_SESSION['admin']['user_id'];
			$ad_log['log_info'] = '广告修改操作';
			$ad_log['ip_address']= get_client_ip();
			$res2 = $admin_log -> add($ad_log);
			$res = $mod -> where('com_id=1')->save($_POST);
			if($res>0){
				$this -> success('更新成功',U('Admin/first/company'));
			}else{
				$this -> error('更新失败',U('Admin/first/company'));
			}	
	}
	//审核现金积分交易
	public function shenhe(){
		$mod = M('market');
		$id = I('get.mk');
		$data['state_check'] = '1';
		$res = $mod -> where('mk_id = "%s"',$id) ->data($data)->save();
		if($res>0){
			$this ->success('审核成功');
		}else{
			$this ->error('审核失败');
		}
	}

}